﻿@model GSOrganization.Application.SysMenuOutput
@{
    Layout = "~/Views/Shared/Create_Layout.cshtml";
}
<style>

</style>
<form class="layui-form" action="" lay-filter="layui">
    <input type="hidden" name="Id" id="Id" value="@ViewBag.Id" />

    <div class="layui-form-item">
        <label class="layui-form-label">父级菜单</label>
        <div class="layui-input-block">
            <a class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" onclick="showModal('../../SysMenu','单选','选择父级菜单');">
                选择
            </a>
            <div id="checkSysMenuId">
            </div>

        </div>

    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">名称</label>
        <div class="layui-input-inline">
            <input type="text" id="Title" name="Title" required lay-verify="required" placeholder="请填写名称" autocomplete="off" class="layui-input" />
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">编码或者链接地址</label>
        <div class="layui-input-inline">
            <input type="text" id="Href" name="Href" placeholder="请填写名称" autocomplete="off" class="layui-input" />
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">排序</label>
        <div class="layui-input-inline">
            <input type="text" id="Sort" name="Sort" required lay-verify="number" placeholder="请填写名称" autocomplete="off" class="layui-input" />
        </div>
    </div>



    <div class="layui-form-item">
        <div class="layui-input-block">

            <button lay-submit lay-filter="formsubmit" class="common_btn" id="submit">提交</button>
            <div class="common_btn" onclick="return window.location.href='../SysMenu'">返回</div>


        </div>
    </div>
</form>

<script>

    var selectedType = "单选"; //父界获取此值，来判断是单选还是多选。参考值：多选，单选

    var parentArray = null;

    //子页面调用父页面的方法，传值
    function selectValue(data) {

        if (data) {
            var content = ""; //需要添加到check显示区域中的内容

            content += '<button value="' + data[0].Id + '" type="button" class="tag-item layui-btn layui-btn-primary layui-btn-sm">'
                + data[0].Name + '<i onclick="deleteLink(this)" class="layui-icon layui-unselect tag-close">ဆ</i></button>';

            parentArray = data;//把子页面传来的值暂存

            $('#checkSysMenuId').html(content);  //覆盖之前的值，赋新值
        }

    }
    //选择项的删除
    function deleteLink(obj) {
        $(obj).parent().remove();
        parentArray.splice(obj, 1)

    }

    layui.use('form', function () {
        var form = layui.form;

        //初始化绑定数据
        var id = $("#Id").val();
        var data = ajaxGet("/api/SysMenu/GetSysMenu/" + id);

        if (data.Succeeded && data.Data != null) {

            var it = data.Data;
            $("#Title").val(it.Title);
            $("#Href").val(it.Href);
            $("#Sort").val(it.Sort);
            if (it.Parent) {
                it.Parent.Name = it.Parent.Title;

                var arr = [];
                arr.push(it.Parent)
                selectValue(arr);
            }
        } else {
            layer.alert(data.Errors, { title: "初始化失败" });
        }



        //监听提交
        form.on('submit(formsubmit)', function (data) {

            var entity = data.field;
            debugger
            if (parentArray) {
                //单选
                entity.ParentId = parentArray[0].Id;
            }

            var data = ajaxPost("/api/SysMenu/EditSysMenu", entity);

            createalert(data, '/SysMenu');

            return false;
        });
    });

</script> 